﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Text;

namespace Cosoware.Web.Documents.SqlClient
{
    internal class SqlRepositoryProvider : RepositoryProvider
    {
        /// <summary>
        /// LINQ data context.
        /// </summary>
        private SqlDataContext _context;

        /// <summary>
        /// LINQ data context.
        /// </summary>
        private String _connectionString;

        /// <summary>
        /// Initializes a new instance of the <see cref="SqlRepositoryProvider"/> class.
        /// </summary>
        public SqlRepositoryProvider()
        {
        }

        /// <summary>
        /// Gets or sets the connection string.
        /// </summary>
        /// <value>The connection string.</value>
        internal string ConnectionString
        {
            get { return _connectionString; }
            set 
            { 
                _connectionString = value;
                InitializeContext();
            }
        }

        /// <summary>
        /// Reads the document.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        protected override Document ReadDocumentInternal(Guid id)
        {
            return _context.Documents.Where(d => d.ID == id).First();
        }

        /// <summary>
        /// Creates the document.
        /// </summary>
        /// <param name="document">The document.</param>
        protected override void CreateDocumentInternal(Document document)
        {
            _context.Documents.InsertOnSubmit(document);
            _context.SubmitChanges();
        }

        /// <summary>
        /// Updates the document.
        /// </summary>
        /// <param name="document">The document.</param>
        protected override void UpdateDocumentInternal(Document document)
        {
            var d = ReadDocumentInternal(document.ID);
            d.ApplyChanges(document);
            _context.SubmitChanges();
        }

        /// <summary>
        /// Deletes the document.
        /// </summary>
        /// <param name="id">The document identifier.</param>
        protected override void DeleteDocumentInternal(Guid id)
        {
            var document = new Document() {ID = id};
            _context.Documents.DeleteOnSubmit(document);
            throw new System.NotImplementedException();
        }

        /// <summary>
        /// Lists the documents that maches the predicate.
        /// </summary>
        /// <param name="predicate">The predicate.</param>
        /// <returns>The enumeration of documents.</returns>
        protected override IEnumerable<Document> ListDocumentsInternal(Func<Document, bool> predicate)
        {
            return _context.Documents.Where(predicate);
        }

        /// <summary>
        /// Counts the documents that maches the predicate.
        /// </summary>
        /// <param name="predicate">The predicate.</param>
        /// <returns>The count of documents.</returns>
        protected override int CountDocumentsInternal(Func<Document, bool> predicate)
        {
            return _context.Documents.Where(predicate).Count();
        }

        /// <summary>
        /// Initializes the context.
        /// </summary>
        private void InitializeContext()
        {
            if (_context == null)
                _context = new SqlDataContext(ConnectionString);
        }
    }
}
